package com.example.demo1.controller;

import com.example.demo1.common.AppResult;
import com.example.demo1.common.ResultCode;
import com.example.demo1.model.Organization;
import com.example.demo1.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Organization")
public class OrganizationController {
    @Autowired
    private OrganizationService organizationService;

    @PostMapping("/create")
    public AppResult<Organization> create(@RequestBody Organization organization) {
        //先对参数进行判断
        if(organization==null){
            return  AppResult.error(ResultCode.BAD_REQUEST);
        }
        return organizationService.create(organization);
    }

    @PostMapping("/basicinfo")
    //这个接口仅仅修改基础信息,不修改父亲节点
    public AppResult<Organization> updateBasicInfo(
             Integer id,
            @RequestBody Organization organization) {
        if(id<=0||organization==null){
            return AppResult.error(ResultCode.BAD_REQUEST);
        }
        organization.setId(id);
        return organizationService.updateBasicInfo(organization);
    }

    @PostMapping("/delete")
    //这里只是删除一个被低级机构引用的机构
    public AppResult<Void> delete(@RequestParam("id") Integer id) {
        System.out.println(id);
        //还是先对参进行判断
        if(id<=0){
            return AppResult.error(ResultCode.BAD_REQUEST);
        }
        return organizationService.delete(id);
    }
//这里是根据id得到对应的组织信息
    @GetMapping("/get")
    public AppResult<Organization> getById(@RequestParam("id") Integer id) {
        //先对参数进行判断
        if(id<=0){
            return AppResult.error(ResultCode.BAD_REQUEST);
        }
        return organizationService.getById(id);
    }
//这里是得到所有的机构信息
    @GetMapping("/getall")
    public AppResult<List<Organization>> getAll() {

        return organizationService.getAll();
    }

    @GetMapping("/ancestors")
    public AppResult<List<Organization>> getAncestors(@RequestParam("id")Integer id) {
        //还是先对参进行判断
        if(id<=0){
            return AppResult.error(ResultCode.BAD_REQUEST);
        }
        return organizationService.getAncestors(id);
    }

    @GetMapping("/descendants")
    public AppResult<List<Organization>> getDescendants(@RequestParam("id")Integer id) {
        //还是先对参进行判断
        if(id<=0){
            return AppResult.error(ResultCode.BAD_REQUEST);
        }
        return organizationService.getDescendants(id);
    }

    @PostMapping("/move")
    public AppResult<Organization> move(
            @RequestParam("id") Integer id,
            @RequestParam(required = false) Integer parentId) {
        //还是先对参进行判断
        if(id<=0){
            return AppResult.error(ResultCode.BAD_REQUEST);
        }
        return organizationService.move(id, parentId);
    }
//    @PostMapping("/with-children")
//    public AppResult<Void> deleteOrganizationWithChildren(@PathVariable Integer id) {
//        return organizationService.deleteOrganizationWithChildren(id);
//    }

}
